/* Copyright (c) 2008, 2009, 2010, 2011 Damian Kmiecik
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE. */

#include <inttypes.h>
#include <util/delay.h>
#include "hardware.h"
#include "spi.h"
#include "flash.h"

#define READ_CMD	0x03
#define HS_READ_CMD	0x0B
#define BYTEPRG_CMD	0x02
#define ERASE_CMD	0x60
#define RDSR_CMD	0x05
#define WREN_CMD	0x06
#define WRDI_CMD	0x04
#define EWSR_CMD	0x50
#define WRSR_CMD	0x01
#define JEDEC_CMD	0x9F
#define AAI_CMD		0xAD

#define BUSY_BIT	0
#define WEL_BIT		1
#define BP0_BIT		2
#define BP1_BIT		3
#define BP2_BIT		4
#define BP3_BIT		5
#define AAI_BIT		6
#define BPL_BIT		7

uint8_t flashReadStatus(void);

void flashInit(void)
{
	FLASH_ENA;
	spiWrite(WREN_CMD);
	FLASH_DIS;
	FLASH_ENA;
	spiWrite(EWSR_CMD);
	FLASH_DIS;
	FLASH_ENA;
	spiWrite(WRSR_CMD);
	spiWrite(0x00);
	FLASH_DIS;
}

void flashErase(void)
{
	FLASH_ENA;
	spiWrite(WREN_CMD);
	FLASH_DIS;
	FLASH_ENA;
	spiWrite(ERASE_CMD);
	FLASH_DIS;
	while (flashReadStatus() & _BV(BUSY_BIT));
}

uint8_t flashReadByte(const uint32_t addr)
{
	while (flashReadStatus() & _BV(BUSY_BIT));
	uint8_t val;
	FLASH_ENA;
	spiWrite(READ_CMD);
	spiWrite(0xFF&(addr>>16));
	spiWrite(0xFF&(addr>>8));
	spiWrite(0xFF&addr);
	val = spiRead();
	FLASH_DIS;
	return val;
}

void flashWriteByte(const uint32_t addr, const uint8_t val)
{
	while (flashReadStatus() & _BV(BUSY_BIT));
	FLASH_ENA;
	spiWrite(WREN_CMD);
	FLASH_DIS;
	FLASH_ENA;
	spiWrite(BYTEPRG_CMD);
	spiWrite(0xFF&(addr>>16));
	spiWrite(0xFF&(addr>>8));
	spiWrite(0xFF&addr);
	spiWrite(val);
	FLASH_DIS;
}

uint8_t flashReadStatus(void)
{
	uint8_t val;
	FLASH_ENA;
	spiWrite(RDSR_CMD);
	val = spiRead();
	FLASH_DIS;
	return val;
}

void flashRead(const uint32_t addr, uint8_t *buf, const uint16_t len)
{
	FLASH_ENA;
	spiWrite(HS_READ_CMD);
	spiWrite(0xFF&(addr>>16));
	spiWrite(0xFF&(addr>>8));
	spiWrite(0xFF&addr);
	spiWrite(0x00);
	uint16_t a = 0;
	while (a < len)
		buf[a++] = spiRead();
	FLASH_DIS;
}

void flashWrite(const uint32_t addr, const uint8_t *buf, const uint16_t len)
{
	for (uint16_t i = 0; i < len; i++)
		flashWriteByte(addr+i, buf[i]);
}
